package org.zcl.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.zcl.entity.model.BasePO;

import java.time.LocalDateTime;

/**
 * @author zhuchenglin
 * @since 2022/9/5 10:24
 */
public class MybatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof BasePO) {
            BasePO originalObj = (BasePO) metaObject.getOriginalObject();

            if (originalObj.getCreateTime() == null) {
                this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.getOriginalObject() instanceof BasePO) {
            BasePO originalObj = (BasePO) metaObject.getOriginalObject();
            if (originalObj.getUpdateTime() == null) {
                this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
            }

        }
    }
}
